package com.fyakm.daodejing.simple.stockTest.util;

import com.fyakm.daodejing.simple.stockTest.domain.StockHistory;
import com.opencsv.CSVReader;

import java.io.FileReader;
import java.util.ArrayList;
import java.util.List;

public class CsvReader {
    // 读取股票历史数据
    public static List<StockHistory> readStockHistoryFromCsv(String filePath) {
        List<StockHistory> historyList = new ArrayList<>();
        try (CSVReader reader = new CSVReader(new FileReader(filePath))) {
            List<String[]> records = reader.readAll();
            // 跳过表头行
            records.remove(0);
            for (String[] record : records) {
                StockHistory history = new StockHistory();
                history.setDate(record[0]);
                history.setCode(record[1]);
                history.setOpen(Double.parseDouble(record[2]));
                history.setHigh(Double.parseDouble(record[3]));
                history.setLow(Double.parseDouble(record[4]));
                history.setClose(Double.parseDouble(record[5]));
                history.setPreclose(Double.parseDouble(record[6]));
                history.setVolume(Double.parseDouble(record[7]));
                history.setAmount(Double.parseDouble(record[8]));
                history.setPctChg(Double.parseDouble(record[9]));
                historyList.add(history);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return historyList;
    }
}